home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsstate.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  29.4 KB  |  939 lines

  1. /* Copyright (C) 1989, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsstate.c,v 1.5.2.2 2000/12/05 09:12:04 raph Exp $ */
  20. /* Miscellaneous graphics state operators for Ghostscript library */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "gserrors.h"
  24. #include "gsstruct.h"
  25. #include "gsutil.h"        /* for gs_next_ids */
  26. #include "gzstate.h"
  27. #include "gxcspace.h"        /* here for gscolor2.h */
  28. #include "gsalpha.h"
  29. #include "gscolor2.h"
  30. #include "gscoord.h"        /* for gs_initmatrix */
  31. #include "gscie.h"
  32. #include "gscssub.h"
  33. #include "gxclipsr.h"
  34. #include "gxcmap.h"
  35. #include "gxdevice.h"
  36. #include "gxpcache.h"
  37. #include "gzht.h"
  38. #include "gzline.h"
  39. #include "gspath.h"
  40. #include "gzpath.h"
  41. #include "gzcpath.h"
  42.  
  43. /* Forward references */
  44. private gs_state *gstate_alloc(P3(gs_memory_t *, client_name_t,
  45.                   const gs_state *));
  46. private gs_state *gstate_clone(P4(gs_state *, gs_memory_t *, client_name_t,
  47.                   gs_state_copy_reason_t));
  48. private void gstate_free_contents(P1(gs_state *));
  49. private int gstate_copy(P4(gs_state *, const gs_state *,
  50.                gs_state_copy_reason_t, client_name_t));
  51.  
  52. /*
  53.  * Graphics state storage management is complicated.  There are many
  54.  * different classes of storage associated with a graphics state:
  55.  *
  56.  * (1) The gstate object itself.  This includes some objects physically
  57.  *      embedded within the gstate object, but because of garbage collection
  58.  *      requirements, there are no embedded objects that can be
  59.  *      referenced by non-transient pointers.  We assume that the gstate
  60.  *      stack "owns" its gstates and that we can free the top gstate when
  61.  *      doing a restore.
  62.  *
  63.  * (2) Objects that are referenced directly by the gstate and whose lifetime
  64.  *      is independent of the gstate.  These are garbage collected, not
  65.  *      reference counted, so we don't need to do anything special with them
  66.  *      when manipulating gstates.  Currently this includes:
  67.  *              font, device
  68.  *
  69.  * (3) Objects that are referenced directly by the gstate, may be shared
  70.  *      among gstates, and should disappear when no gstates reference them.
  71.  *      These fall into two groups:
  72.  *
  73.  *   (3a) Objects that are logically connected to individual gstates.
  74.  *      We use reference counting to manage these.  Currently these are:
  75.  *              halftone, dev_ht, cie_render, black_generation,
  76.  *              undercolor_removal, set_transfer.*, cie_joint_caches,
  77.  *        clip_stack, {opacity,shape}.mask
  78.  *      effective_transfer.* may point to some of the same objects as
  79.  *      set_transfer.*, but don't contribute to the reference count.
  80.  *      Similarly, dev_color may point to the dev_ht object.  For
  81.  *      simplicity, we initialize all of these pointers to 0 and then
  82.  *      allocate the object itself when needed.
  83.  *
  84.  *   (3b) Objects whose lifetimes are associated with something else.
  85.  *      Currently these are:
  86.  *              ht_cache, which is associated with the entire gstate
  87.  *                stack, is allocated with the very first graphics state,
  88.  *                and currently is never freed;
  89.  *              pattern_cache, which is associated with the entire
  90.  *                stack, is allocated when first needed, and currently
  91.  *                is never freed;
  92.  *              view_clip, which is associated with the current
  93.  *                save level (effectively, with the gstate sub-stack
  94.  *                back to the save) and is managed specially;
  95.  *        transparency_stack, which is associated with the entire
  96.  *          stack but only stored in the topmost graphics state.
  97.  *
  98.  * (4) Objects that are referenced directly by exactly one gstate and that
  99.  *      are not referenced (except transiently) from any other object.
  100.  *      These fall into two groups:
  101.  *
  102.  *   (4b) Objects allocated individually, for the given reason:
  103.  *              line_params.dash.pattern (variable-length),
  104.  *              color_space, path, clip_path, effective_clip.path,
  105.  *              ccolor, dev_color
  106.  *                  (may be referenced from image enumerators or elsewhere)
  107.  *
  108.  *   (4b) The "client data" for a gstate.  For the interpreter, this is
  109.  *      the refs associated with the gstate, such as the screen procedures.
  110.  *      Client-supplied procedures manage client data.
  111.  *
  112.  * (5) Objects referenced indirectly from gstate objects of category (4),
  113.  *      including objects that may also be referenced directly by the gstate.
  114.  *      The individual routines that manipulate these are responsible
  115.  *      for doing the right kind of reference counting or whatever.
  116.  *      Currently:
  117.  *              path, clip_path, and (if different from both clip_path
  118.  *                and view_clip) effective_clip.path require
  119.  *                gx_path_assign/free, which uses a reference count;
  120.  *              color_space and ccolor require cs_adjust_color/cspace_count
  121.  *                or cs_adjust_counts, which use a reference count;
  122.  *              dev_color has no references to storage that it owns.
  123.  *      We count on garbage collection or restore to deallocate
  124.  *        sub-objects of halftone.
  125.  *
  126.  * Note that when after a gsave, the existing gstate references the related
  127.  * objects that we allocate at the same time, and the newly allocated gstate
  128.  * references the old related objects.  Similarly, during a grestore, we
  129.  * free the related objects referenced by the current gstate, but after the
  130.  * grestore, we free the saved gstate, not the current one.  However, when
  131.  * we allocate gstates off-stack, the newly allocated gstate does reference
  132.  * the newly allocated component objects.  Note also that setgstate /
  133.  * currentgstate may produce gstates in which different allocators own
  134.  * different sub-objects; this is OK, because restore guarantees that there
  135.  * won't be any dangling pointers (as long as we don't allow pointers from
  136.  * global gstates to local objects).
  137.  */
  138.  
  139. /*
  140.  * Enumerate the pointers in a graphics state, other than the ones in the
  141.  * imager state, and device, which must be handled specially.
  142.  */
  143. #define gs_state_do_ptrs(m)\
  144.   m(0,saved) m(1,path) m(2,clip_path) m(3,clip_stack)\
  145.   m(4,view_clip) m(5,effective_clip_path)\
  146.   m(6,color_space) m(7,ccolor) m(8,dev_color)\
  147.   m(9,font) m(10,root_font) m(11,show_gstate) /*m(---,device)*/\
  148.   m(12,transparency_group_stack)
  149. #define gs_state_num_ptrs 13
  150.  
  151. /*
  152.  * Define these elements of the graphics state that are allocated
  153.  * individually for each state, except for line_params.dash.pattern.
  154.  * Note that effective_clip_shared is not on the list.
  155.  */
  156. typedef struct gs_state_parts_s {
  157.     gx_path *path;
  158.     gx_clip_path *clip_path;
  159.     gx_clip_path *effective_clip_path;
  160.     gs_color_space *color_space;
  161.     gs_client_color *ccolor;
  162.     gx_device_color *dev_color;
  163. } gs_state_parts;
  164.  
  165. #define GSTATE_ASSIGN_PARTS(pto, pfrom)\
  166.   ((pto)->path = (pfrom)->path, (pto)->clip_path = (pfrom)->clip_path,\
  167.    (pto)->effective_clip_path = (pfrom)->effective_clip_path,\
  168.    (pto)->color_space = (pfrom)->color_space,\
  169.    (pto)->ccolor = (pfrom)->ccolor, (pto)->dev_color = (pfrom)->dev_color)
  170.  
  171. /* GC descriptors */
  172. extern_st(st_imager_state);
  173. public_st_gs_state();
  174.  
  175. /* GC procedures for gs_state */
  176. private ENUM_PTRS_WITH(gs_state_enum_ptrs, gs_state *gsvptr)
  177. ENUM_PREFIX(st_imager_state, gs_state_num_ptrs + 1);
  178. #define e1(i,elt) ENUM_PTR(i,gs_state,elt);
  179. gs_state_do_ptrs(e1)
  180. case gs_state_num_ptrs:    /* handle device specially */
  181. ENUM_RETURN(gx_device_enum_ptr(gsvptr->device));
  182. #undef e1
  183. ENUM_PTRS_END
  184. private RELOC_PTRS_WITH(gs_state_reloc_ptrs, gs_state *gsvptr)
  185. {
  186.     RELOC_PREFIX(st_imager_state);
  187.     {
  188. #define r1(i,elt) RELOC_PTR(gs_state,elt);
  189.     gs_state_do_ptrs(r1)
  190. #undef r1
  191.     gsvptr->device = gx_device_reloc_ptr(gsvptr->device, gcst);
  192.     }
  193. }
  194. RELOC_PTRS_END
  195.  
  196. /* Copy client data, using the copy_for procedure if available, */
  197. /* the copy procedure otherwise. */
  198. private int
  199. gstate_copy_client_data(gs_state * pgs, void *dto, void *dfrom,
  200.             gs_state_copy_reason_t reason)
  201. {
  202.     return (pgs->client_procs.copy_for != 0 ?
  203.         (*pgs->client_procs.copy_for) (dto, dfrom, reason) :
  204.         (*pgs->client_procs.copy) (dto, dfrom));
  205. }
  206.  
  207. /* ------ Operations on the entire graphics state ------ */
  208.  
  209. /* Define the initial value of the graphics state. */
  210. private const gs_imager_state gstate_initial = {
  211.     gs_imager_state_initial(1.0)
  212. };
  213.  
  214. /*
  215.  * Allocate a path for the graphics state.  We use stable memory because
  216.  * some PostScript files have Type 3 fonts whose BuildChar procedure
  217.  * uses the sequence save ... setcachedevice ... restore, and the path
  218.  * built between the setcachedevice and the restore must not be freed.
  219.  * If it weren't for this, we don't think stable memory would be needed.
  220.  */
  221. private gs_memory_t *
  222. gstate_path_memory(gs_memory_t *mem)
  223. {
  224.     return gs_memory_stable(mem);
  225. }
  226.  
  227. /* Allocate and initialize a graphics state. */
  228. gs_state *
  229. gs_state_alloc(gs_memory_t * mem)
  230. {
  231.     gs_state *pgs = gstate_alloc(mem, "gs_state_alloc", NULL);
  232.     int code;
  233.  
  234.     if (pgs == 0)
  235.     return 0;
  236.     pgs->saved = 0;
  237.     *(gs_imager_state *)pgs = gstate_initial;
  238.  
  239.     /*
  240.      * Just enough of the state is initialized at this point
  241.      * that it's OK to call gs_state_free if an allocation fails.
  242.      */
  243.  
  244.     code = gs_imager_state_initialize((gs_imager_state *) pgs, mem);
  245.     if (code < 0)
  246.     goto fail;
  247.  
  248.     /* Finish initializing the color rendering state. */
  249.  
  250.     rc_alloc_struct_1(pgs->halftone, gs_halftone, &st_halftone, mem,
  251.               goto fail, "gs_state_alloc(halftone)");
  252.     pgs->halftone->type = ht_type_none;
  253.     pgs->ht_cache = gx_ht_alloc_cache(mem,
  254.                       gx_ht_cache_default_tiles(),
  255.                       gx_ht_cache_default_bits());
  256.  
  257.     /* Initialize other things not covered by initgraphics */
  258.  
  259.     pgs->path = gx_path_alloc(gstate_path_memory(mem), "gs_state_alloc(path)");
  260.     pgs->clip_path = gx_cpath_alloc(mem, "gs_state_alloc(clip_path)");
  261.     pgs->clip_stack = 0;
  262.     pgs->view_clip = gx_cpath_alloc(mem, "gs_state_alloc(view_clip)");
  263.     pgs->view_clip->rule = 0;    /* no clipping */
  264.     pgs->effective_clip_id = pgs->clip_path->id;
  265.     pgs->effective_view_clip_id = gs_no_id;
  266.     pgs->effective_clip_path = pgs->clip_path;
  267.     pgs->effective_clip_shared = true;
  268.     /* Initialize things so that gx_remap_color won't crash. */
  269.     gs_cspace_init_DeviceGray(pgs->color_space);
  270.     {
  271.     int i;
  272.  
  273.     for (i = 0; i < countof(pgs->device_color_spaces.indexed); ++i)
  274.         pgs->device_color_spaces.indexed[i] = 0;
  275.     }
  276.     gx_set_device_color_1(pgs);
  277.     pgs->device = 0;        /* setting device adjusts refcts */
  278.     gs_nulldevice(pgs);
  279.     gs_setalpha(pgs, 1.0);
  280.     gs_settransfer(pgs, gs_identity_transfer);
  281.     gs_setflat(pgs, 1.0);
  282.     gs_setfilladjust(pgs, 0.25, 0.25);
  283.     gs_setlimitclamp(pgs, false);
  284.     gs_setstrokeadjust(pgs, true);
  285.     pgs->font = 0;        /* Not right, but acceptable until the */
  286.     /* PostScript code does the first setfont. */
  287.     pgs->root_font = 0;        /* ditto */
  288.     pgs->in_cachedevice = 0;
  289.     pgs->in_charpath = (gs_char_path_mode) 0;
  290.     pgs->show_gstate = 0;
  291.     pgs->level = 0;
  292.     pgs->transparency_group_stack = 0;
  293.     if (gs_initgraphics(pgs) >= 0)
  294.     return pgs;
  295.     /* Something went very wrong. */
  296. fail:
  297.     gs_state_free(pgs);
  298.     return 0;
  299. }
  300.  
  301. /* Set the client data in a graphics state. */
  302. /* This should only be done to a newly created state. */
  303. void
  304. gs_state_set_client(gs_state * pgs, void *pdata,
  305.             const gs_state_client_procs * pprocs)
  306. {
  307.     pgs->client_data = pdata;
  308.     pgs->client_procs = *pprocs;
  309. }
  310.  
  311. /* Get the client data from a graphics state. */
  312. #undef gs_state_client_data    /* gzstate.h makes this a macro */
  313. void *
  314. gs_state_client_data(const gs_state * pgs)
  315. {
  316.     return pgs->client_data;
  317. }
  318.  
  319. /* Free a graphics state. */
  320. int
  321. gs_state_free(gs_state * pgs)
  322. {
  323.     gstate_free_contents(pgs);
  324.     gs_free_object(pgs->memory, pgs, "gs_state_free");
  325.     return 0;
  326. }
  327.  
  328. /* Save the graphics state. */
  329. int
  330. gs_gsave(gs_state * pgs)
  331. {
  332.     gs_state *pnew = gstate_clone(pgs, pgs->memory, "gs_gsave",
  333.                   copy_for_gsave);
  334.  
  335.     if (pnew == 0)
  336.     return_error(gs_error_VMerror);
  337.     /*
  338.      * It isn't clear from the Adobe documentation whether gsave retains
  339.      * the current clip stack or clears it.  The following statement
  340.      * bets on the latter.  If it's the former, this should become
  341.      *    rc_increment(pnew->clip_stack);
  342.      */
  343.     pnew->clip_stack = 0;
  344.     pgs->saved = pnew;
  345.     if (pgs->show_gstate == pgs)
  346.     pgs->show_gstate = pnew->show_gstate = pnew;
  347.     pgs->level++;
  348.     if_debug2('g', "[g]gsave -> 0x%lx, level = %d\n",
  349.           (ulong) pnew, pgs->level);
  350.     return 0;
  351. }
  352.  
  353. /*
  354.  * Save the graphics state for a 'save'.
  355.  * We cut the stack below the new gstate, and return the old one.
  356.  * In addition to an ordinary gsave, we create a new view clip path.
  357.  */
  358. int
  359. gs_gsave_for_save(gs_state * pgs, gs_state ** psaved)
  360. {
  361.     int code;
  362.     gx_clip_path *old_cpath = pgs->view_clip;
  363.     gx_clip_path *new_cpath;
  364.     gx_device_color_spaces_t save_spaces;
  365.     int i;
  366.  
  367.     if (old_cpath) {
  368.     new_cpath =
  369.         gx_cpath_alloc_shared(old_cpath, pgs->memory,
  370.                   "gs_gsave_for_save(view_clip)");
  371.     if (new_cpath == 0)
  372.         return_error(gs_error_VMerror);
  373.     } else {
  374.     new_cpath = 0;
  375.     }
  376.     code = gs_gsave(pgs);
  377.     if (code < 0)
  378.     goto fail;
  379.     for (i = 0; i < countof(save_spaces.indexed); ++i) {
  380.     gs_color_space *pcs = pgs->device_color_spaces.indexed[i];
  381.  
  382.     if (pcs) {
  383.         pgs->device_color_spaces.indexed[i] = 0;
  384.         code = gs_setsubstitutecolorspace(pgs, (gs_color_space_index)i,
  385.                           pcs);
  386.         if (code < 0) {
  387.         /*
  388.          * Patch the second saved pointer so we won't try to
  389.          * gsave after the grestore.
  390.          */
  391.         if (pgs->saved->saved == 0)
  392.             pgs->saved->saved = pgs;
  393.         gs_grestore(pgs);
  394.         if (pgs->saved == pgs)
  395.             pgs->saved = 0;
  396.         goto fail;
  397.         }
  398.     }
  399.     }
  400.     if (pgs->effective_clip_path == pgs->view_clip)
  401.     pgs->effective_clip_path = new_cpath;
  402.     pgs->view_clip = new_cpath;
  403.     /* Cut the stack so we can't grestore past here. */
  404.     *psaved = pgs->saved;
  405.     pgs->saved = 0;
  406.     return code;
  407. fail:
  408.     if (new_cpath)
  409.     gx_cpath_free(new_cpath, "gs_gsave_for_save(view_clip)");
  410.     return code;
  411. }
  412.  
  413. /* Restore the graphics state. Can fully empty graphics stack */
  414. int    /* return 0 if ok, 1 if stack was empty */
  415. gs_grestore_only(gs_state * pgs)
  416. {
  417.     gs_state *saved = pgs->saved;
  418.     void *pdata = pgs->client_data;
  419.     void *sdata;
  420.     gs_transparency_state_t *tstack = pgs->transparency_stack;
  421.  
  422.     if_debug2('g', "[g]grestore 0x%lx, level was %d\n",
  423.           (ulong) saved, pgs->level);
  424.     if (!saved)
  425.     return 1;
  426.     sdata = saved->client_data;
  427.     if (saved->pattern_cache == 0)
  428.     saved->pattern_cache = pgs->pattern_cache;
  429.     /* Swap back the client data pointers. */
  430.     pgs->client_data = sdata;
  431.     saved->client_data = pdata;
  432.     if (pdata != 0 && sdata != 0)
  433.     gstate_copy_client_data(pgs, pdata, sdata, copy_for_grestore);
  434.     gstate_free_contents(pgs);
  435.     *pgs = *saved;
  436.     pgs->transparency_stack = tstack;
  437.     if (pgs->show_gstate == saved)
  438.     pgs->show_gstate = pgs;
  439.     gs_free_object(pgs->memory, saved, "gs_grestore");
  440.     return 0;
  441. }
  442.  
  443. /* Restore the graphics state per PostScript semantics */
  444. int
  445. gs_grestore(gs_state * pgs)
  446. {
  447.     int code;
  448.     if (!pgs->saved)
  449.     return gs_gsave(pgs);    /* shouldn't ever happen */
  450.     code = gs_grestore_only(pgs);
  451.     if (code < 0)
  452.     return code;
  453.  
  454.     /* Wraparound: make sure there are always >= 1 saves on stack */
  455.     if (pgs->saved)
  456.     return 0;
  457.     return gs_gsave(pgs);
  458. }
  459.  
  460. /* Restore the graphics state for a 'restore', splicing the old stack */
  461. /* back on.  Note that we actually do a grestoreall + 2 grestores. */
  462. int
  463. gs_grestoreall_for_restore(gs_state * pgs, gs_state * saved)
  464. {
  465.     int code;
  466.     gx_device_color_spaces_t freed_spaces;
  467.  
  468.     while (pgs->saved->saved) {
  469.     code = gs_grestore(pgs);
  470.     if (code < 0)
  471.         return code;
  472.     }
  473.     /* Make sure we don't leave dangling pointers in the caches. */
  474.     gx_ht_clear_cache(pgs->ht_cache);
  475.     if (pgs->pattern_cache)
  476.     (*pgs->pattern_cache->free_all) (pgs->pattern_cache);
  477.     pgs->saved->saved = saved;
  478.     freed_spaces = pgs->device_color_spaces;
  479.     code = gs_grestore(pgs);
  480.     if (code < 0)
  481.     return code;
  482.     gx_device_color_spaces_free(&freed_spaces, pgs->memory,
  483.                 "gs_grestoreall_for_restore");
  484.     if (pgs->view_clip) {
  485.     gx_cpath_free(pgs->view_clip, "gs_grestoreall_for_restore");
  486.     pgs->view_clip = 0;
  487.     }
  488.     return gs_grestore(pgs);
  489. }
  490.  
  491.  
  492. /* Restore to the bottommost graphics state (at this save level). */
  493. int
  494. gs_grestoreall(gs_state * pgs)
  495. {
  496.     if (!pgs->saved)        /* shouldn't happen */
  497.     return gs_gsave(pgs);
  498.     while (pgs->saved->saved) {
  499.     int code = gs_grestore(pgs);
  500.  
  501.     if (code < 0)
  502.         return code;
  503.     }
  504.     return gs_grestore(pgs);
  505. }
  506.  
  507. /* Allocate and return a new graphics state. */
  508. gs_state *
  509. gs_gstate(gs_state * pgs)
  510. {
  511.     gs_state *copied = gs_state_copy(pgs, pgs->memory);
  512.     int i;
  513.  
  514.     if (copied == 0)
  515.     return 0;
  516.     /* Don't capture the substituted color spaces. */
  517.     for (i = 0; i < countof(copied->device_color_spaces.indexed); ++i)
  518.     copied->device_color_spaces.indexed[i] = 0;
  519.     return copied;
  520. }
  521. gs_state *
  522. gs_state_copy(gs_state * pgs, gs_memory_t * mem)
  523. {
  524.     gs_state *pnew;
  525.     /* Prevent 'capturing' the view clip path. */
  526.     gx_clip_path *view_clip = pgs->view_clip;
  527.  
  528.     pgs->view_clip = 0;
  529.     pnew = gstate_clone(pgs, mem, "gs_gstate", copy_for_gstate);
  530.     rc_increment(pnew->clip_stack);
  531.     pgs->view_clip = view_clip;
  532.     if (pnew == 0)
  533.     return 0;
  534.     pnew->saved = 0;
  535.     /*
  536.      * Prevent dangling references from the show_gstate pointer.  If
  537.      * this context is its own show_gstate, set the pointer in the clone
  538.      * to point to the clone; otherwise, set the pointer in the clone to
  539.      * 0, and let gs_setgstate fix it up.
  540.      */
  541.     pnew->show_gstate =
  542.     (pgs->show_gstate == pgs ? pnew : 0);
  543.     return pnew;
  544. }
  545.  
  546. /* Copy one previously allocated graphics state to another. */
  547. int
  548. gs_copygstate(gs_state * pto, const gs_state * pfrom)
  549. {
  550.     return gstate_copy(pto, pfrom, copy_for_copygstate, "gs_copygstate");
  551. }
  552.  
  553. /* Copy the current graphics state to a previously allocated one. */
  554. int
  555. gs_currentgstate(gs_state * pto, const gs_state * pgs)
  556. {
  557.     int code =
  558.     gstate_copy(pto, pgs, copy_for_currentgstate, "gs_currentgstate");
  559.  
  560.     if (code >= 0)
  561.     pto->view_clip = 0;
  562.     return code;
  563. }
  564.  
  565. /* Restore the current graphics state from a previously allocated one. */
  566. int
  567. gs_setgstate(gs_state * pgs, const gs_state * pfrom)
  568. {
  569.     /*
  570.      * The implementation is the same as currentgstate,
  571.      * except we must preserve the saved pointer, the level,
  572.      * the view clip, and possibly the show_gstate.
  573.      */
  574.     gs_state *saved_show = pgs->show_gstate;
  575.     int level = pgs->level;
  576.     gx_clip_path *view_clip = pgs->view_clip;
  577.     gs_transparency_state_t *tstack = pgs->transparency_stack;
  578.     int code;
  579.  
  580.     pgs->view_clip = 0;        /* prevent refcount decrementing */
  581.     code = gstate_copy(pgs, pfrom, copy_for_setgstate, "gs_setgstate");
  582.     if (code < 0)
  583.     return code;
  584.     pgs->level = level;
  585.     pgs->view_clip = view_clip;
  586.     pgs->show_gstate =
  587.     (pgs->show_gstate == pfrom ? pgs : saved_show);
  588.     pgs->transparency_stack = tstack;
  589.     return 0;
  590. }
  591.  
  592. /* Get the allocator pointer of a graphics state. */
  593. /* This is provided only for the interpreter */
  594. /* and for color space implementation. */
  595. gs_memory_t *
  596. gs_state_memory(const gs_state * pgs)
  597. {
  598.     return pgs->memory;
  599. }
  600.  
  601. /* Get the saved pointer of the graphics state. */
  602. /* This is provided only for Level 2 grestore. */
  603. gs_state *
  604. gs_state_saved(const gs_state * pgs)
  605. {
  606.     return pgs->saved;
  607. }
  608.  
  609. /* Swap the saved pointer of the graphics state. */
  610. /* This is provided only for save/restore. */
  611. gs_state *
  612. gs_state_swap_saved(gs_state * pgs, gs_state * new_saved)
  613. {
  614.     gs_state *saved = pgs->saved;
  615.  
  616.     pgs->saved = new_saved;
  617.     return saved;
  618. }
  619.  
  620. /* Swap the memory pointer of the graphics state. */
  621. /* This is provided only for the interpreter. */
  622. gs_memory_t *
  623. gs_state_swap_memory(gs_state * pgs, gs_memory_t * mem)
  624. {
  625.     gs_memory_t *memory = pgs->memory;
  626.  
  627.     pgs->memory = mem;
  628.     return memory;
  629. }
  630.  
  631. /* ------ Operations on components ------ */
  632.  
  633. /* Reset most of the graphics state */
  634. int
  635. gs_initgraphics(gs_state * pgs)
  636. {
  637.     int code;
  638.  
  639.     gs_initmatrix(pgs);
  640.     if ((code = gs_newpath(pgs)) < 0 ||
  641.     (code = gs_initclip(pgs)) < 0 ||
  642.     (code = gs_setlinewidth(pgs, 1.0)) < 0 ||
  643.     (code = gs_setlinecap(pgs, gstate_initial.line_params.cap)) < 0 ||
  644.     (code = gs_setlinejoin(pgs, gstate_initial.line_params.join)) < 0 ||
  645.     (code = gs_setcurvejoin(pgs, gstate_initial.line_params.curve_join)) < 0 ||
  646.     (code = gs_setdash(pgs, (float *)0, 0, 0.0)) < 0 ||
  647.     (gs_setdashadapt(pgs, false),
  648.      (code = gs_setdotlength(pgs, 0.0, false))) < 0 ||
  649.     (code = gs_setdotorientation(pgs)) < 0 ||
  650.     (code = gs_setgray(pgs, 0.0)) < 0 ||
  651.     (code = gs_setmiterlimit(pgs, gstate_initial.line_params.miter_limit)) < 0
  652.     )
  653.     return code;
  654.     gs_init_rop(pgs);
  655.     return 0;
  656. }
  657.  
  658. /* setfilladjust */
  659. int
  660. gs_setfilladjust(gs_state * pgs, floatp adjust_x, floatp adjust_y)
  661. {
  662. #define CLAMP_TO_HALF(v)\
  663.     ((v) <= 0 ? fixed_0 : (v) >= 0.5 ? fixed_half : float2fixed(v));
  664.  
  665.     pgs->fill_adjust.x = CLAMP_TO_HALF(adjust_x);
  666.     pgs->fill_adjust.y = CLAMP_TO_HALF(adjust_y);
  667.     return 0;
  668. #undef CLAMP_TO_HALF
  669. }
  670.  
  671. /* currentfilladjust */
  672. int
  673. gs_currentfilladjust(const gs_state * pgs, gs_point * adjust)
  674. {
  675.     adjust->x = fixed2float(pgs->fill_adjust.x);
  676.     adjust->y = fixed2float(pgs->fill_adjust.y);
  677.     return 0;
  678. }
  679.  
  680. /* setlimitclamp */
  681. void
  682. gs_setlimitclamp(gs_state * pgs, bool clamp)
  683. {
  684.     pgs->clamp_coordinates = clamp;
  685. }
  686.  
  687. /* currentlimitclamp */
  688. bool
  689. gs_currentlimitclamp(const gs_state * pgs)
  690. {
  691.     return pgs->clamp_coordinates;
  692. }
  693.  
  694. /* ------ Internal routines ------ */
  695.  
  696. /* Free the privately allocated parts of a gstate. */
  697. private void
  698. gstate_free_parts(const gs_state * parts, gs_memory_t * mem, client_name_t cname)
  699. {
  700.     gs_free_object(mem, parts->dev_color, cname);
  701.     gs_free_object(mem, parts->ccolor, cname);
  702.     gs_free_object(mem, parts->color_space, cname);
  703.     if (!parts->effective_clip_shared)
  704.     gx_cpath_free(parts->effective_clip_path, cname);
  705.     gx_cpath_free(parts->clip_path, cname);
  706.     gx_path_free(parts->path, cname);
  707. }
  708.  
  709. /* Allocate the privately allocated parts of a gstate. */
  710. private int
  711. gstate_alloc_parts(gs_state * parts, const gs_state * shared,
  712.            gs_memory_t * mem, client_name_t cname)
  713. {
  714.     gs_memory_t *path_mem = gstate_path_memory(mem);
  715.  
  716.     parts->path =
  717.     (shared ?
  718.      gx_path_alloc_shared(shared->path, path_mem,
  719.                   "gstate_alloc_parts(path)") :
  720.      gx_path_alloc(path_mem, "gstate_alloc_parts(path)"));
  721.     parts->clip_path =
  722.     (shared ?
  723.      gx_cpath_alloc_shared(shared->clip_path, mem,
  724.                    "gstate_alloc_parts(clip_path)") :
  725.      gx_cpath_alloc(mem, "gstate_alloc_parts(clip_path)"));
  726.     if (!shared || shared->effective_clip_shared) {
  727.     parts->effective_clip_path = parts->clip_path;
  728.     parts->effective_clip_shared = true;
  729.     } else {
  730.     parts->effective_clip_path =
  731.         gx_cpath_alloc_shared(shared->effective_clip_path, mem,
  732.                   "gstate_alloc_parts(effective_clip_path)");
  733.     parts->effective_clip_shared = false;
  734.     }
  735.     parts->color_space =
  736.     gs_alloc_struct(mem, gs_color_space, &st_color_space, cname);
  737.     parts->ccolor =
  738.     gs_alloc_struct(mem, gs_client_color, &st_client_color, cname);
  739.     parts->dev_color =
  740.     gs_alloc_struct(mem, gx_device_color, &st_device_color, cname);
  741.     if (parts->path == 0 || parts->clip_path == 0 ||
  742.     parts->effective_clip_path == 0 ||
  743.     parts->color_space == 0 || parts->ccolor == 0 ||
  744.     parts->dev_color == 0
  745.     ) {
  746.     gstate_free_parts(parts, mem, cname);
  747.     return_error(gs_error_VMerror);
  748.     }
  749.     return 0;
  750. }
  751.  
  752. /*
  753.  * Allocate a gstate and its contents.
  754.  * If pfrom is not NULL, the path, clip_path, and (if distinct from both
  755.  * clip_path and view_clip) effective_clip_path share the segments of
  756.  * pfrom's corresponding path(s).
  757.  */
  758. private gs_state *
  759. gstate_alloc(gs_memory_t * mem, client_name_t cname, const gs_state * pfrom)
  760. {
  761.     gs_state *pgs =
  762.     gs_alloc_struct(mem, gs_state, &st_gs_state, cname);
  763.  
  764.     if (pgs == 0)
  765.     return 0;
  766.     if (gstate_alloc_parts(pgs, pfrom, mem, cname) < 0) {
  767.     gs_free_object(mem, pgs, cname);
  768.     return 0;
  769.     }
  770.     pgs->memory = mem;
  771.     return pgs;
  772. }
  773.  
  774. /* Copy the dash pattern from one gstate to another. */
  775. private int
  776. gstate_copy_dash(gs_state * pto, const gs_state * pfrom)
  777. {
  778.     return gs_setdash(pto, pfrom->line_params.dash.pattern,
  779.               pfrom->line_params.dash.pattern_size,
  780.               pfrom->line_params.dash.offset);
  781. }
  782.  
  783. /* Clone an existing graphics state. */
  784. /* Return 0 if the allocation fails. */
  785. /* If reason is for_gsave, the clone refers to the old contents, */
  786. /* and we switch the old state to refer to the new contents. */
  787. private gs_state *
  788. gstate_clone(gs_state * pfrom, gs_memory_t * mem, client_name_t cname,
  789.          gs_state_copy_reason_t reason)
  790. {
  791.     gs_state *pgs = gstate_alloc(mem, cname, pfrom);
  792.     gs_state_parts parts;
  793.  
  794.     if (pgs == 0)
  795.     return 0;
  796.     GSTATE_ASSIGN_PARTS(&parts, pgs);
  797.     *pgs = *pfrom;
  798.     pgs->transparency_stack = 0;
  799.     /* Copy the dash pattern if necessary. */
  800.     if (pgs->line_params.dash.pattern) {
  801.     int code;
  802.  
  803.     pgs->line_params.dash.pattern = 0;    /* force allocation */
  804.     code = gstate_copy_dash(pgs, pfrom);
  805.     if (code < 0)
  806.         goto fail;
  807.     }
  808.     if (pgs->client_data != 0) {
  809.     void *pdata = pgs->client_data = (*pgs->client_procs.alloc) (mem);
  810.  
  811.     if (pdata == 0 ||
  812.      gstate_copy_client_data(pgs, pdata, pfrom->client_data, reason) < 0
  813.         )
  814.         goto fail;
  815.     }
  816.     gs_imager_state_copied((gs_imager_state *)pgs);
  817.     /* Don't do anything to clip_stack. */
  818.     rc_increment(pgs->device);
  819.     *parts.color_space = *pfrom->color_space;
  820.     *parts.ccolor = *pfrom->ccolor;
  821.     *parts.dev_color = *pfrom->dev_color;
  822.     if (reason == copy_for_gsave) {
  823.     float *dfrom = pfrom->line_params.dash.pattern;
  824.     float *dto = pgs->line_params.dash.pattern;
  825.  
  826.     GSTATE_ASSIGN_PARTS(pfrom, &parts);
  827.     pgs->line_params.dash.pattern = dfrom;
  828.     pfrom->line_params.dash.pattern = dto;
  829.     } else {
  830.     GSTATE_ASSIGN_PARTS(pgs, &parts);
  831.     }
  832.     cs_adjust_counts(pgs, 1);
  833.     return pgs;
  834.   fail:
  835.     gs_free_object(mem, pgs->line_params.dash.pattern, cname);
  836.     GSTATE_ASSIGN_PARTS(pgs, &parts);
  837.     gstate_free_parts(pgs, mem, cname);
  838.     gs_free_object(mem, pgs, cname);
  839.     return 0;
  840. }
  841.  
  842. /* Release the composite parts of a graphics state, */
  843. /* but not the state itself. */
  844. private void
  845. gstate_free_contents(gs_state * pgs)
  846. {
  847.     gs_memory_t *mem = pgs->memory;
  848.     const char *const cname = "gstate_free_contents";
  849.  
  850.     rc_decrement(pgs->device, cname);
  851.     rc_decrement(pgs->clip_stack, cname);
  852.     cs_adjust_counts(pgs, -1);
  853.     if (pgs->client_data != 0)
  854.     (*pgs->client_procs.free) (pgs->client_data, mem);
  855.     gs_free_object(mem, pgs->line_params.dash.pattern, cname);
  856.     gstate_free_parts(pgs, mem, cname);
  857.     gs_imager_state_release((gs_imager_state *)pgs);
  858. }
  859.  
  860. /* Copy one gstate to another. */
  861. private int
  862. gstate_copy(gs_state * pto, const gs_state * pfrom,
  863.         gs_state_copy_reason_t reason, client_name_t cname)
  864. {
  865.     gs_state_parts parts;
  866.     gx_device_color_spaces_t saved_spaces;
  867.  
  868.     GSTATE_ASSIGN_PARTS(&parts, pto);
  869.     saved_spaces = pto->device_color_spaces;
  870.     /* Copy the dash pattern if necessary. */
  871.     if (pfrom->line_params.dash.pattern || pto->line_params.dash.pattern) {
  872.     int code = gstate_copy_dash(pto, pfrom);
  873.  
  874.     if (code < 0)
  875.         return code;
  876.     }
  877.     /*
  878.      * It's OK to decrement the counts before incrementing them,
  879.      * because anything that is going to survive has a count of
  880.      * at least 2 (pto and somewhere else) initially.
  881.      * Handle references from contents.
  882.      */
  883.     cs_adjust_counts(pto, -1);
  884.     gx_path_assign_preserve(pto->path, pfrom->path);
  885.     gx_cpath_assign_preserve(pto->clip_path, pfrom->clip_path);
  886.     /*
  887.      * effective_clip_shared will be copied, but we need to do the
  888.      * right thing with effective_clip_path.
  889.      */
  890.     if (pfrom->effective_clip_shared) {
  891.     /*
  892.      * pfrom->effective_clip_path is either pfrom->view_clip or
  893.      * pfrom->clip_path.
  894.      */
  895.     parts.effective_clip_path =
  896.         (pfrom->effective_clip_path == pfrom->view_clip ?
  897.          pto->view_clip : parts.clip_path);
  898.     } else
  899.     gx_cpath_assign_preserve(pto->effective_clip_path,
  900.                  pfrom->effective_clip_path);
  901.     *parts.color_space = *pfrom->color_space;
  902.     *parts.ccolor = *pfrom->ccolor;
  903.     *parts.dev_color = *pfrom->dev_color;
  904.     cs_adjust_counts(pto, 1);
  905.     /* Handle references from gstate object. */
  906. #define RCCOPY(element)\
  907.     rc_pre_assign(pto->element, pfrom->element, cname)
  908.     RCCOPY(device);
  909.     RCCOPY(clip_stack);
  910.     {
  911.     struct gx_pattern_cache_s *pcache = pto->pattern_cache;
  912.     void *pdata = pto->client_data;
  913.     gs_memory_t *mem = pto->memory;
  914.     gs_state *saved = pto->saved;
  915.     float *pattern = pto->line_params.dash.pattern;
  916.  
  917.     gs_imager_state_pre_assign((gs_imager_state *)pto,
  918.                    (gs_imager_state *)pfrom);
  919.     *pto = *pfrom;
  920.     pto->client_data = pdata;
  921.     pto->memory = mem;
  922.     pto->saved = saved;
  923.     pto->line_params.dash.pattern = pattern;
  924.     if (pto->pattern_cache == 0)
  925.         pto->pattern_cache = pcache;
  926.     if (pfrom->client_data != 0) {
  927.         /* We need to break 'const' here. */
  928.         gstate_copy_client_data((gs_state *) pfrom, pdata,
  929.                     pfrom->client_data, reason);
  930.     }
  931.     }
  932.     GSTATE_ASSIGN_PARTS(pto, &parts);
  933.     pto->device_color_spaces = saved_spaces;
  934. #undef RCCOPY
  935.     pto->show_gstate =
  936.     (pfrom->show_gstate == pfrom ? pto : 0);
  937.     return 0;
  938. }
  939.